﻿using System;
using System.Collections.Generic;
using System.Text;
using MCromwell.StaffIntranet.DependencyInjection;
using MCromwell.StaffIntranet.Task.Infrastructure.Logging;
using MCromwell.StaffIntranet.Core;
using MCromwell.StaffIntranet.Data.NHibernate;
using Mapper;
using MCromwell.StaffIntranet.DTO;
using log4net.altconf;
using log4net.Config;
using System.IO;

namespace MCromwell.StaffIntranet.Task
{
    public static class TaskLayerBootStrapper
    {
        public static void RegisterMappers()
        {
            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<Administrator, AdministratorDTO>());
            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<Location, LocationDTO>());

            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<Staff, StaffDTO>()
                                                        .Explicit((staff, staffDTO) => staffDTO.LocationId = staff.Location.Id)
                                                        .Explicit((staff, staffDTO) => staffDTO.LocationDescription = staff.Location.Value)
                                                        .Explicit((staff, staffDTO) => staffDTO.Photo.Data = staff.Photo));

            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<StaffDTO, Staff>()
                                                        .Explicit((staffDTO, staff) =>
                                                        {
                                                            if ((staffDTO.Photo.SaveOption == PhotoSaveOption.Update) && (staffDTO.Photo.Data != null))
                                                                staff.Photo = staffDTO.Photo.Data;
                                                        })
                                                        .Explicit((staffDTO, staff) => staff.Location.Id = staffDTO.LocationId));

            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<DomainObjectValidationResult, NotificationDTO>()
                                                        .Explicit((domain, dto) =>
                                                        {
                                                            foreach (var item in domain.Results)
                                                                dto.AddError(item.ValidationId, item.Message);
                                                        }));
        }
        

        public static void ConfigureDependencies()
        {
            IoC.AddComponent<ILog, Log4NetLog>("Log");
            IoC.AddComponent<IStaffRepository, NHibernateStaffRepository>("StaffRepository");
            IoC.AddComponent<ILookupRepository, NHibernateLookupRepository>("LookupRepository");
            IoC.AddComponent<IAdministrationRepository, NHibernateAdministrationRepository>("AdministrationRepository");
        }

        public static void ConfigureLogging()
        {
            DslConfigurator.BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            DslConfigurator.Configure("logging-config.boo");
        }
    }
}
